package org.example.ssh

import net.schmizz.sshj.SSHClient
import net.schmizz.sshj.sftp.SFTPClient
import org.example.ssh.base.*
import java.io.File

// 传到目标server（本地部署的服务器） 打包镜像 -> 上传镜像到仓库

class BuildImgAndUp {
    fun main(ssh: SSHClient) {
        val deployDir = "/home/mydesk-deploy"
        val dataDir = "/home/mydesk-data"
        val localDocDir = "./docker/v1"
        val localDoc = File(localDocDir)

        // region
        val sftp: SFTPClient = ssh.newSFTPClient()

        if(!exists(sftp, deployDir)) {
            mkdirs(sftp, deployDir)
        }
        if(!exists(sftp, dataDir)) {
            mkdirs(sftp, dataDir)
        }

        val remoteDeployDir = "$deployDir/${localDoc.name}"
        deleteDirectoryRecursive(sftp, remoteDeployDir)
        sftp.mkdir(remoteDeployDir)

        uploadDirectoryRecursive(sftp, localDocDir, deployDir)
        println("远程compose相关文件已上传成功")
        // endregion

        var res = executeCmd(ssh, "cd $remoteDeployDir && docker-compose build")
        println(res)
        res = executeCmd(ssh, "docker images")
        println(res)
        println("打包镜像完成")

        println("上传镜像文件开始")
    }
}